<template>
  <div class="main">
    <div class="head">
      <el-card>
        <!-- 头部部分 -->
        <div class="head-list">
          学科编号<el-input v-model="form.rid"
                    class="inputf"></el-input>

          学科名称<el-input v-model="form.name"
                    class="inputf"></el-input>
          创建者
          <el-input v-model="form.username"
                    class="inputf"></el-input>
          状态
          <el-select v-model="form.status"
                     class="inputf">
            <el-option v-for="item in list"
                       :key="item.value"
                       :label="item.label"
                       :value="item.value"></el-option>
          </el-select>

          <el-button type="primary"
                     @click="searchData">搜索</el-button>
          <el-button class="btn"
                     @click="clean">清除</el-button>
          <el-button type="primary"
                     class="btn"
                     @click="add">+新增学科</el-button>
        </div>
      </el-card>
    </div>

    <div class="body">
      <!-- 身体部分 -->
      <el-card>
        <el-table :data="tableData">
          <el-table-column label="序号"
                           sortable
                           type="index">
            <template v-slot="{ $index }">
              <div>
                {{ (form.page - 1) * form.limit + $index + 1 }}
              </div>
            </template>
          </el-table-column>
          <el-table-column label="学科编号"
                           sortable
                           prop="rid"></el-table-column>
          <el-table-column label="学科名称"
                           sortable
                           prop="name"></el-table-column>
          <el-table-column label="学科简称"
                           sortable
                           prop="short_name"></el-table-column>
          <el-table-column label="创建者"
                           sortable
                           prop="username"></el-table-column>
          <el-table-column label="创建日期"
                           sortable
                           prop="create_time"></el-table-column>
          <el-table-column label="状态"
                           prop="status">
            <template v-slot="{ row }">
              <div :class="{ red: row.status == 0 }">
                {{ row.status == 0 ? '禁用' : '启用' }}
              </div>
            </template>
          </el-table-column>
          <el-table-column label="操作"
                           fixed="right">
            <template v-slot="{ row }">
              <div class="list"
                   style="width: 120px">
                <el-button type="text"
                           @click="editEvent(row)">编辑</el-button>
                <el-button type="text"
                           @click="changestatus(row.id)">
                  {{ row.status == 1 ? '禁用' : '启用' }}
                </el-button>
                <el-button type="text"
                           @click="del(row.id)">删除</el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <div class="footer">
          <el-pagination @size-change="sizeChange"
                         @current-change="pageChange"
                         :current-page="form.page"
                         :page-sizes="[1, 10, 20, 50, 100]"
                         :page-size="form.limit"
                         :total="form.total"
                         layout="total,sizes,prev,pager,next,jumper"></el-pagination>
        </div>
      </el-card>
    </div>

    <Dialog v-model="isShow"
            ref="Dialog"
            @getData="getData"></Dialog>
  </div>
</template>
<script>
import Dialog from './components/dialog.vue'
import { subjectList, subjectRemove, subjectStatus } from '@/api/studylist'

export default {
  components: {
    Dialog
  },
  data () {
    return {
      isShow: false,
      form: {
        rid: '',
        name: '',
        username: '',
        status: '',
        page: 1,
        total: 100,
        limit: 100
      },
      list: [
        { value: '1', label: '启用' },
        {
          value: '2',
          label: '禁用'
        }
      ],

      tableData: [{}]
    }
  },

  methods: {
    add () {
      // 弹框展示
      this.isShow = true
      this.$refs.Dialog.meta = 'add'
    },
    // 学科删除
    del (id) {
      this.$confirm('确认删除数据', '提示').then(async () => {
        await subjectRemove({ id })
        this.$message.success('删除成功')
        this.form.page = 1
        this.getData()
      })
    },
    // 清除数据
    clean () {
      // eslint-disable-next-line no-unused-expressions
      this.form.rid = ''
      this.form.name = ''
      this.form.username = ''
      this.form.status = ''
      this.getData()
    },
    sizeChange (size) {
      this.form.limit = size
      this.form.page = 1
      this.getData()
    },
    pageChange (page) {
      this.form.page = page
      this.getData()
    },
    // 获取数据
    async getData () {
      const res = await subjectList(this.form)
      console.log('书籍列表', res)
      this.tableData = res.data.items
      this.form.total = res.data.pagination.total
    },
    // 搜索数据
    searchData () {
      this.getData()
    },
    editEvent (row) {
      this.add()
      this.$refs.Dialog.meta = 'edit'
      this.$refs.Dialog.form = JSON.parse(JSON.stringify(row))
    },
    async changestatus (id) {
      const res = await subjectStatus({ id })
      console.log('状态', res)

      this.getData()
      this.$refs.Dialog.form.status = this.form.status
    }
  },
  created () {
    this.getData()
  }
}
</script>

<style style="less">
.main {
  margin: 20px;
}

.head-list {
  width: 100%;
  display: flex;
  height: 50px;
  text-align: center;
  align-items: center;
}
.body {
  margin-top: 15px;
}
.footer {
  text-align: center;
}
.inputf {
  width: 100px;
  height: 39px;
}
.btn {
  margin-left: 15px;
  height: 39px;
}
.red {
  color: red;
}
</style>
